package com.hanxiaozhang.order.controller;

import com.hanxiaozhang.annotation.EnableGlobalException;
import com.hanxiaozhang.order.entity.Order;
import com.hanxiaozhang.exception.InternalServerException;
import com.hanxiaozhang.order.repository.OrderRepository;
import com.hanxiaozhang.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


/**
 * 功能描述: <br>
 * 〈OrderController〉
 *
 * @Author:hanxinghua
 * @Date: 2020/7/19
 */
@Slf4j
@Controller
@RequestMapping("/order")
//@EnableGlobalException
public class OrderController {

  @Autowired
  private OrderRepository orderRepository;

  @GetMapping("/{id}")
  @ResponseBody
  public Result<Order> findById(@PathVariable("id") Long id) {
    Order order = orderRepository.findOne(id);
    if(id==1){
      throw new InternalServerException();
//      throw new NullPointerException();
    }
    return Result.success(order);
  }



  @ResponseBody
  @PostMapping("/save")
  public Result save(@RequestBody Order order){
    orderRepository.save(order);
    return Result.success();
  }


}
